﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WinForm = System.Windows.Forms;
using CutPhotos.Properties;
using Prism.Commands;
using Prism.Mvvm;
using VNext;
using VNext.Log4Net;
using IO = System.IO;
using Prism.Dialogs;
using System.Windows.Shell;
using VNext.Tools;
using System.Windows.Shapes;
using System.IO;

namespace CutPhotos
{
    public class MainViewModel : BindableBase
    {
        private Logger Logger;
        public MainViewModel(Logger Logger)
        {
            this.Logger = Logger;
            CutPhotosCommand = new DelegateCommand(CutPhotos);
        }

        public DelegateCommand CutPhotosCommand { get; private set; }

        private void CutPhotos()
        {
            using (var dialog = new WinForm.FolderBrowserDialog())
            {
                dialog.Description = "请选择一个文件夹";
                dialog.ShowNewFolderButton = true; // 是否允许用户新建文件夹
                WinForm.DialogResult result = dialog.ShowDialog();

                try
                {
                    if (result == WinForm.DialogResult.OK && !string.IsNullOrWhiteSpace(dialog.SelectedPath))
                    {
                        string folderPath = dialog.SelectedPath;
                        PhotoCutHelper.Init("100,320;1900,320;1827,1799;321,1809");
                        var folderPathNew = $"{folderPath}_cut";
                        FielHelper.CreateDirctory(folderPathNew, true);

                        #region 这是通过名字分拣好坏图片用的临时代码
                        //var folderPathBase = "C:\\Users\\shakasi\\Documents\\WeChat Files\\jason-si\\FileStorage\\File\\2024-09";
                        //folderPath = IO.Path.Combine(folderPathBase, "photos");
                        #endregion

                        string[] files = IO.Directory.GetFiles(folderPath, "*.jpg", SearchOption.TopDirectoryOnly);
                        foreach (var file in files)
                        {
                            var fileName = IO.Path.GetFileName(file);
                            #region 这是通过名字分拣好坏图片用的临时代码
                            //if (file.Contains("_before"))
                            //{
                            //    File.Copy(file, IO.Path.Combine(folderPathBase, $"bad\\{fileName}"));
                            //}
                            //else
                            //{
                            //    File.Copy(file, IO.Path.Combine(folderPathBase, $"good\\{fileName}"));
                            //}
                            #endregion
                            var image = PhotoCutHelper.CutPhoto(file);
                            File.WriteAllBytes(IO.Path.Combine(folderPathNew, fileName), image);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"裁剪图片报错：{ex.Message}");
                }
                MessageBox.Show($"批量裁剪图片成功");
            }
        }
    }
}
